#include "../inc/g711toaac.h"
// #include "../lib/libfaac/faac/frame.h"
// #include "../inc/frame.h"
const int CON_PCM_SIZE = 320;

/* copy from CCITT G.711 specifications */
unsigned char _u2a[128] = {			/* u- to A-law conversions */
	1,	1,	2,	2,	3,	3,	4,	4,
	5,	5,	6,	6,	7,	7,	8,	8,
	9,	10,	11,	12,	13,	14,	15,	16,
	17,	18,	19,	20,	21,	22,	23,	24,
	25,	27,	29,	31,	33,	34,	35,	36,
	37,	38,	39,	40,	41,	42,	43,	44,
	46,	48,	49,	50,	51,	52,	53,	54,
	55,	56,	57,	58,	59,	60,	61,	62,
	64,	65,	66,	67,	68,	69,	70,	71,
	72,	73,	74,	75,	76,	77,	78,	79,
	81,	82,	83,	84,	85,	86,	87,	88,
	89,	90,	91,	92,	93,	94,	95,	96,
	97,	98,	99,	100,	101,	102,	103,	104,
	105,	106,	107,	108,	109,	110,	111,	112,
	113,	114,	115,	116,	117,	118,	119,	120,
	121,	122,	123,	124,	125,	126,	127,	128
};

unsigned char _a2u[128] = {			/* A- to u-law conversions */
	1,	3,	5,	7,	9,	11,	13,	15,
	16,	17,	18,	19,	20,	21,	22,	23,
	24,	25,	26,	27,	28,	29,	30,	31,
	32,	32,	33,	33,	34,	34,	35,	35,
	36,	37,	38,	39,	40,	41,	42,	43,
	44,	45,	46,	47,	48,	48,	49,	49,
	50,	51,	52,	53,	54,	55,	56,	57,
	58,	59,	60,	61,	62,	63,	64,	64,
	65,	66,	67,	68,	69,	70,	71,	72,
	73,	74,	75,	76,	77,	78,	79,	79,
	80,	81,	82,	83,	84,	85,	86,	87,
	88,	89,	90,	91,	92,	93,	94,	95,
	96,	97,	98,	99,	100,	101,	102,	103,
	104,	105,	106,	107,	108,	109,	110,	111,
	112,	113,	114,	115,	116,	117,	118,	119,
	120,	121,	122,	123,	124,	125,	126,	127
};

/*
 author: sjz
 time：2021-12-22
 func: g711 ----> aac
*/


/* 封装对外的接口 */
/* 传入编码器对象，构建编码器 */
int Easy_AACEncoder_Init(void * encodectx, int initType,int g711FrameLen)
{
    G7ToAac *encoder = (G7ToAac *)encodectx;
    InAudioInfo m_info;
    m_info.m_u32AudioCodec = Law_ALaw;
    m_info.m_u32AudioSamplerate = 8000;    // 8K 采样率 
    m_info.m_u32AudioChannel = 1;          // 单通道 
    m_info.m_nPCMBitSize = 16;             // 采样精度
    /* 编码器初始化 */
	printf(" [ %s | %d ] start init encode \n",__FILE__,__LINE__);
    encoder->nRet = 0;
	encoder->nTmp = 0;
	encoder->nCount = 0;
	encoder->nStatus = 0;
	encoder->nPCMRead = 0;
    encoder->m_pbPCMBuffer = NULL;
    encoder->m_pbAACBuffer = NULL;
	encoder->m_pbG7FrameBuffer = NULL;
    encoder->m_pbPCMTmpBuffer = NULL;
    encoder->m_audio_buffer_ = NULL;
	encoder->m_pDecodeToPcm = NULL;
	encoder->m_pPCMToAAC = NULL;
    encoder->m_inAudioInfo = m_info;

    /* 根据解码器类型创建解码对象 ，将 g711 转换成 PCM */    
    if(encoder->m_inAudioInfo.m_u32AudioCodec == Law_ALaw)
    {
        encoder->m_pDecodeToPcm = (IDecodeToPcm *) malloc(sizeof(IDecodeToPcm));
    }
    else if(encoder->m_inAudioInfo.m_u32AudioCodec == Law_ULaw)
    {
        encoder->m_pDecodeToPcm = (IDecodeToPcm *) malloc(sizeof(IDecodeToPcm));
    } 
    else if(encoder->m_inAudioInfo.m_u32AudioCodec == Law_G726)
    {
        /* 暂时不做 726 */
        return 0;
    }
 
    encoder->m_pDecodeToPcm->m_g7FrameSize = g711FrameLen; /* 一帧 G711 的大小        160 */
    encoder->m_pDecodeToPcm->Decode = g711Decode;    /* 解码器 */
    /* 编码器对象 */
    encoder->m_pPCMToAAC = (PcmToAac*)malloc(sizeof(PcmToAac));
    encoder->m_pPCMToAAC->m_nPCMBitSize = 16;
    encoder->m_pPCMToAAC->m_nInputSamples = 0;
    encoder->m_pPCMToAAC->m_nMaxOutputBytes = 0;
    unsigned int nChannels = m_info.m_u32AudioChannel;     // 单通道
	unsigned long nInputSamples = 0;
	unsigned long nSampleRate = m_info.m_u32AudioSamplerate;    // 8000 采样率
	unsigned long nMaxOutputBytes = 0;

    /*open FAAC engine*/
    encoder->m_pPCMToAAC->hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);
	printf(" [ %s | %d ] nInputSamples = %ld, nMaxOutputBytes = %ld\n", __FILE__,__LINE__, nInputSamples, nMaxOutputBytes);       // 1024, 768
    encoder->m_pPCMToAAC->m_nInputSamples = nInputSamples;   /* 获取样本 */
	encoder->m_pPCMToAAC->m_nMaxOutputBytes = nMaxOutputBytes;

    /*0 - raw; 1 - ADTS*/
    encoder->m_pPCMToAAC->pConfiguration = (faacEncConfiguration*)malloc(sizeof(faacEncConfiguration));
	encoder->m_pPCMToAAC->pConfiguration->inputFormat = FAAC_INPUT_16BIT;
	encoder->m_pPCMToAAC->pConfiguration->outputFormat = 1;
	encoder->m_pPCMToAAC->pConfiguration->useTns = 0;
	encoder->m_pPCMToAAC->pConfiguration->aacObjectType = LOW;
	encoder->m_pPCMToAAC->pConfiguration->mpegVersion = MPEG4;

    /* 设置编码器配置 */
	faacEncSetConfiguration(encoder->m_pPCMToAAC->hEncoder, encoder->m_pPCMToAAC->pConfiguration);

    /* 设置 PCM 数据的缓存区 ;缓存一个样本的数据 */
    encoder->m_nPCMBufferSize = encoder->m_pPCMToAAC->m_nInputSamples * (encoder->m_pPCMToAAC->m_nPCMBitSize / 8);
	encoder->m_pbPCMBuffer = (unsigned char*) malloc(encoder->m_nPCMBufferSize * sizeof (unsigned char));
	memset(encoder->m_pbPCMBuffer, 0, encoder->m_nPCMBufferSize);

	encoder->m_nMaxOutputBytes = encoder->m_pPCMToAAC->m_nMaxOutputBytes;
	encoder->m_pbAACBuffer = (unsigned char*) malloc(encoder->m_nMaxOutputBytes * sizeof (unsigned char));
	memset(encoder->m_pbAACBuffer, 0, encoder->m_nMaxOutputBytes);  // 解码的编码的缓存 768 
	encoder->m_nG7FrameBufferSize = encoder->m_pDecodeToPcm->m_g7FrameSize;   /* g711 一帧缓存大小 160 */
	encoder->m_pbG7FrameBuffer = (unsigned char *) malloc(encoder->m_nG7FrameBufferSize * sizeof (unsigned char));
	memset(encoder->m_pbG7FrameBuffer, 0, encoder->m_nG7FrameBufferSize);

	// encoder->m_nPCMSize = CON_PCM_SIZE;    /*  320 */  // 单帧 PCM 数据缓存区
	encoder->m_nPCMSize = encoder->m_pDecodeToPcm->m_g7FrameSize * 2;
	encoder->m_pbPCMTmpBuffer = (unsigned char *) malloc(encoder->m_nPCMSize * sizeof (unsigned char));
	memset(encoder->m_pbPCMTmpBuffer, 0, encoder->m_nPCMSize);

    /* 2K 缓存区,后面数据就在这里面一帧一帧出来-----蓄水池 */
	if(initType == FILETYPE)
	{
		encoder->m_audio_buffer_ = (audio_buffer *)malloc(sizeof(audio_buffer));
		encoder->m_audio_buffer_->data_ = (unsigned char *)malloc(2*1024 * sizeof(unsigned char));
		encoder->m_audio_buffer_->sumLen_ =2 * 1024 *sizeof(unsigned char);
		encoder->m_audio_buffer_->len_ = 0;
		printf(" [ %s | %d ] start init audio buffer \n",__FILE__,__LINE__);
	}
    return 1;
}
/* 编码 每次传入 512字节的数据 */
int Easy_AACEncoder_Encode(void* handle, unsigned char* inbuf, unsigned int inlen, unsigned char* outbuf, unsigned int* outlen)
{
    G7ToAac *ctx = (G7ToAac *)handle;
    if(ctx == NULL)
    {
        return -1;
    }
    if (NULL != ctx->m_pDecodeToPcm)
	{
        /* 编解码 */
        if(g711_t_aac_decode(ctx,inbuf,inlen,outbuf,outlen) > 0)
        {
            return *outlen;
        }
    }
    return 0;
}
/* 释放资源 */
void Easy_AACEncoder_Release(void* handle)
{
    G7ToAac *encoder = (G7ToAac *)handle;
    faacEncClose(encoder->m_pPCMToAAC->hEncoder);
    if(encoder != NULL)
    {
        free(encoder->m_pDecodeToPcm);
        free(encoder->m_pPCMToAAC->pConfiguration);
        free(encoder->m_pPCMToAAC);
		if(NULL != encoder->m_pbPCMBuffer)
        	free(encoder->m_pbPCMBuffer);
		if(NULL != encoder->m_pbAACBuffer)
        	free(encoder->m_pbAACBuffer);
        free(encoder->m_pbG7FrameBuffer);
		if(NULL != encoder->m_pbPCMTmpBuffer)
        	free(encoder->m_pbPCMTmpBuffer);
		if((NULL != encoder->m_audio_buffer_) && (NULL != encoder->m_audio_buffer_->data_))
        	free(encoder->m_audio_buffer_->data_);
		if(NULL != encoder->m_audio_buffer_)
			free(encoder->m_audio_buffer_);
    }
}

/*------------------------------------------------解码到编码的流程------------------------------------------------------*/

/* 每次传入 512 字节数据，每次赛选一帧数据解码处理，解码后的数据存到 PCM 缓存区中 ，之后统一编码，推到AAC缓存区，再到文件中*/
int g711_t_aac_decode(void * para,void * data,int len, unsigned char* outbuf, unsigned int* outlen )
{
    int buffer_len = 0;
    int nPCMSize = 0;
    *outlen = 0;
    G7ToAac * ctx = (G7ToAac *)para;
    /* 数据缓存;有 1K 的空间做缓存;每次进来 512 字节数据 */
    write_2_pcmbuffer(ctx,data,len);
    /* 一帧一帧取出来做处理 160 字节的空间 */
    while(buffer_len = write_2_pcm_frame(ctx,ctx->m_pbG7FrameBuffer,ctx->m_nG7FrameBufferSize))
    {
		// printf("%s:[%d] get a frame g711 data ,len is %d !\n", __FILE__, __LINE__,buffer_len);
        if (buffer_len <= 0)
		{
			 printf("%s:[%d] G711A -> PCM  no G711 data !\n", __FILE__, __LINE__);
			return -1;
		}
        ctx->nStatus = 0;
        nPCMSize = ctx->m_nPCMSize;
        /* 返回解码数据的大小 */
        if ((ctx->nPCMRead = ctx->m_pDecodeToPcm->Decode(ctx->m_pbPCMTmpBuffer, (unsigned int*)&nPCMSize, ctx->m_pbG7FrameBuffer, buffer_len )) < 0) // jiema
		{
			 printf("%s:[%d] G711A -> PCM  failed buffer_len = %d !\n", __FILE__, __LINE__, buffer_len);            
			return -1;
		}
        /* PCM 的缓存区内存够不够 ；如果不够*/
        if ((ctx->m_nPCMBufferSize - ctx->nCount) < ctx->nPCMRead)
		{
			ctx->nStatus = 1;
			memset(ctx->m_pbAACBuffer, 0, ctx->m_nMaxOutputBytes); // 清空 AAC 缓存区
			memcpy(ctx->m_pbPCMBuffer + ctx->nCount, ctx->m_pbPCMTmpBuffer, (ctx->m_nPCMBufferSize - ctx->nCount));  /* 将解码出来的当前帧的 PCM 数据加入PCM 缓存区；填满缓存区 */
			/* 编码 这个时候 PCM 缓存区已经存满了数据，进行编码，输出编码数据到 m_pbAACBuffer */
			// printf("%s:[%d] --%d--%d---- !\n", __FILE__, __LINE__,ctx->m_nPCMBufferSize,ctx->m_nMaxOutputBytes);
			ctx->nRet = aac_Encode(para, (int32_t*)ctx->m_pbPCMBuffer , ctx->m_nPCMBufferSize , ctx->m_pbAACBuffer, ctx->m_nMaxOutputBytes);
            /* 将 PCM 编码的*/
			// printf("%s:[%d] %d %d !\n", __FILE__, __LINE__,*outlen,ctx->nRet);
			memcpy(outbuf + *outlen, ctx->m_pbAACBuffer, ctx->nRet);
			*outlen += ctx->nRet;

			ctx->nTmp = ctx->nPCMRead - (ctx->m_nPCMBufferSize - ctx->nCount);    /* 没写完的那个 PCM 帧，还有多少没有写进去 */
			memset(ctx->m_pbPCMBuffer, 0, ctx->m_nPCMBufferSize);   /* PCM 缓存区的数据都编码完后，进行清空 */
			memcpy(ctx->m_pbPCMBuffer, ctx->m_pbPCMTmpBuffer + (ctx->m_nPCMBufferSize - ctx->nCount), ctx->nTmp);   /* 将那一帧没有存完的，存到下一个编码缓存区上 */
			ctx->nCount = 0;  // 重新开始计数写入了多少数据到 PCM 缓存区
			ctx->nCount += ctx->nTmp;   /* 记录残留PCM帧写进缓存区的大小 */
		}
		/* 将解码的数据推向 PCM 缓存区*/
        if (ctx->nStatus == 0)
		{
			memcpy(ctx->m_pbPCMBuffer + ctx->nCount, ctx->m_pbPCMTmpBuffer, ctx->nPCMRead);  /* PCM内存够用，就直接将解码的数据往里面丢 */
			ctx->nCount += ctx->nPCMRead;
			// printf("%s:[%d] G711A -> PCM nCount = %d\n",  __FILE__, __LINE__ , ctx->nCount);
		}
        /* 数据都读取完后 */
        if (ctx->nPCMRead < /*320*/ctx->m_nPCMSize)
		{
			// printf("%s:[%d] ____________nPCMRead = %d\n",  __FILE__, __LINE__, ctx->nPCMRead);
			ctx->nRet = aac_Encode(para,(int*) ctx->m_pbPCMBuffer, ctx->nCount , ctx->m_pbAACBuffer, ctx->m_nMaxOutputBytes);
			memcpy(outbuf + *outlen, ctx->m_pbAACBuffer, ctx->nRet);
			*outlen += ctx->nRet;
		}
    }
    return *outlen;
}

/* 往水池丢 */
int write_2_pcmbuffer(G7ToAac *gta, void* data , int len)
{
    if(gta->m_audio_buffer_->sumLen_ - gta->m_audio_buffer_->len_ < len)
    {
        printf("[ %s  %d  ]  m_audio_buffer_ buffer is small !!!\n",__FILE__,__LINE__);
        return -1;
    }
    else {
        if(len > 0)
        {
            memcpy(gta->m_audio_buffer_->data_ + gta->m_audio_buffer_->len_, data, len);
            gta->m_audio_buffer_->len_ += len;
            return len;
        }
    }
    return 0;
}

/* 取出一帧出来 */
int write_2_pcm_frame(G7ToAac *gta, unsigned char* dest , int len)
{
    if (gta->m_audio_buffer_->len_ == 0 || gta->m_audio_buffer_->len_ < len)
	{
		return 0;   // 当里面的数据不够的时候 
	}
	else
	{
		memcpy(dest, gta->m_audio_buffer_->data_, len);   /* 取出一帧数据出来 */
		memmove(gta->m_audio_buffer_->data_, gta->m_audio_buffer_->data_ + len, gta->m_audio_buffer_->len_ - len);
		gta->m_audio_buffer_->len_ -= len;
		return len;
	}
}


// 按帧进行转码
int g711_t_aac_frame_decode(void * para,const unsigned char * inframe_data,const int infream_len,unsigned char *AacFrame_data,int *aac_len)
{
	G7ToAac * ctx = (G7ToAac *)para;
	*aac_len = 0;
	// 直接解码
	// printf("%s:[%d] start decode !\n", __FILE__, __LINE__);
	if ((ctx->nPCMRead = ctx->m_pDecodeToPcm->Decode(ctx->m_pbPCMTmpBuffer, (unsigned int*)&ctx->m_nPCMSize, inframe_data, infream_len )) < 0) // jiema
	{
		printf("%s:[%d] G711A -> PCM  failed buffer_len = %d !\n", __FILE__, __LINE__, infream_len);     
		return -1;
	}
	/* 将解码的当前帧 PCM 数据存到 PCM缓存区，获取一个样本的数据 */
	/* 当一个样本的数据快满了的时候 */
	if ((ctx->m_nPCMBufferSize - ctx->nCount) < ctx->nPCMRead)
	{
		memcpy(ctx->m_pbPCMBuffer + ctx->nCount, ctx->m_pbPCMTmpBuffer, (ctx->m_nPCMBufferSize - ctx->nCount));  /* 将解码出来的当前帧的 PCM 数据加入PCM 缓存区；填满缓存区 */
		/* 编码 这个时候 PCM 缓存区已经存满了数据，进行编码，输出编码数据到 m_pbAACBuffer */
		// printf("%s:[%d] --%d--%d---- !\n", __FILE__, __LINE__,ctx->m_nPCMBufferSize,ctx->m_nMaxOutputBytes);
		ctx->nRet = aac_Encode(para, (int32_t*)ctx->m_pbPCMBuffer , ctx->m_nPCMBufferSize , ctx->m_pbAACBuffer, ctx->m_nMaxOutputBytes);
		// printf("%s:[%d] %d %d !\n", __FILE__, __LINE__,*aac_len,ctx->nRet);
		memcpy(AacFrame_data + *aac_len, ctx->m_pbAACBuffer, ctx->nRet);
		*aac_len += ctx->nRet;
		ctx->nTmp = ctx->nPCMRead - (ctx->m_nPCMBufferSize - ctx->nCount);    /* 没写完的那个 PCM 帧，还有多少没有写进去 */
		memset(ctx->m_pbPCMBuffer, 0, ctx->m_nPCMBufferSize);   /* PCM 缓存区的数据都编码完后，进行清空 */
		memcpy(ctx->m_pbPCMBuffer, ctx->m_pbPCMTmpBuffer + (ctx->m_nPCMBufferSize - ctx->nCount), ctx->nTmp);   /* 将那一帧没有存完的，存到下一个编码缓存区上 */
		ctx->nCount = 0;  // 重新开始计数写入了多少数据到 PCM 缓存区
		ctx->nCount += ctx->nTmp;   /* 记录残留PCM帧写进缓存区的大小 */
	}
	else {
		/* 样本没有满，就继续往里面推数据 */
		memcpy(ctx->m_pbPCMBuffer + ctx->nCount, ctx->m_pbPCMTmpBuffer, ctx->nPCMRead);
		ctx->nCount += ctx->nPCMRead;
		// printf("%s:[%d] --%d--%d---- !\n", __FILE__, __LINE__,ctx->m_nPCMBufferSize,ctx->nCount);
		// *aac_len = 0;
	}
	if (ctx->nPCMRead < /*320*/ctx->m_nPCMSize)
	{
		// printf("%s:[%d] ___________________________nPCMRead = %d\n",  __FILE__, __LINE__, ctx->nPCMRead);
		ctx->nRet = aac_Encode(para,(int*) ctx->m_pbPCMBuffer, ctx->nCount , ctx->m_pbAACBuffer, ctx->m_nMaxOutputBytes);
		memcpy(AacFrame_data + *aac_len, ctx->m_pbAACBuffer, ctx->nRet);
		*aac_len += ctx->nRet;
		return ctx->nRet;
	}
	return *aac_len;
}



/*--------------------------------------------------------- G711a 解码 --------------------------------------------------------------*/

int g711Decode(unsigned char* pout_buf, unsigned int* pout_len , unsigned char* pin_buf, unsigned int in_len)
{
	int16_t *dst = (int16_t *) pout_buf;
	uint8_t *src = (uint8_t *) pin_buf;
	uint32_t i = 0;
	int Ret = 0;

	if ((NULL == pout_buf) || (NULL == pout_len) || (NULL == pin_buf) || (0 == in_len))
	{
		return -1;
	}
	if (*pout_len < 2 * in_len)
	{
		return -2;
	}
	//---{{{
	for (i = 0; i < in_len; i++)
	{
		*(dst++) = (int16_t)DecodeOneChar(*(src++));
	}
	//---}}}
	*pout_len = 2 * in_len;

	Ret = 2 * in_len;
	return Ret;
}

unsigned short DecodeOneChar(unsigned char data)
{
	return (int16_t)alaw2linear(data);
}

int alaw2linear(unsigned char	a_val)
{
	int		t;
	int		seg;

	a_val ^= 0x55;

	t = (a_val & 0x0f) << 4;
	seg = ((unsigned)a_val & 0x70) >> 4;
	switch (seg) {
	case 0:
		t += 8;
		break;
	case 1:
		t += 0x108;
		break;
	default:
		t += 0x108;
		t <<= seg - 1;
	}
	return ((a_val & 0x80) ? t : -t);
}

/*--------------------------------------------------------- AAC 编码 --------------------------------------------------------------*/
int aac_Encode(void* para,int32_t * pbPCMBuffer, unsigned int nPCMBufferSize, unsigned char * pbAACBuffer, unsigned int nMaxOutputBytes)
{
    G7ToAac *ctx = (G7ToAac *)para;
	unsigned int nPCMBitSize = ctx->m_pPCMToAAC->m_nPCMBitSize;    /* 获取编码的 AAC 是多少位的 */
	unsigned int nInputSamples = (nPCMBufferSize / (nPCMBitSize / 8)); /* 编码后的帧大小 */
	printf("%s:[%d] G711A -> AAC faacEncEncode sample size .... %d \n",  __FILE__, __LINE__,nInputSamples);
    /* 编码 */
	int nRet = faacEncEncode(ctx->m_pPCMToAAC->hEncoder, (int*) pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);
	printf("%s:[%d] PCM -> AAC sample change size .... %d \n", __FILE__, __LINE__,nRet);
	return nRet;
}

/* u-law to A-law */
unsigned charulaw2alaw(unsigned char	uval)
{
	uval &= 0xff;
	return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
}


/* A-law to u-law */
unsigned charalaw2ulaw(unsigned char	aval)
{
	aval &= 0xff;
	return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : (0x7F ^ _a2u[aval ^ 0x55]));
}


/*--------------------------------------------------------- 测试代码 --------------------------------------------------------------*/
/*
    流程：
        1.创建编解码器
        2.读取 G711 数据到缓存区 ；
        3.将缓存区的数据写入 对象的G711缓存池中，
        4.每次取出一帧数据进行解码，得到一帧PCM数据
        5.将PCM裸流存入 PCM 缓存区，当满足一个样本的数据量的时候 
        6.进行编码，将其编成 AAC 数据，然后写入 AAC 缓存区中
        7.将AAC 缓存区的数据写入文件中
*/
/* 测试代码 */
int g711_2_aac_test(const char *infile, const char * outfile)
{
	uint8_t pbG711ABuffer[512];  // G711数据缓存区
	uint8_t pbAACBuffer[1024];   // AAC 数据缓存区
    G7ToAac encodectx;
	/* AAC 编码初始化；返回G711转换成AAC的编码器 */
	Easy_AACEncoder_Init(&encodectx,FILETYPE,160);   // 按文件来处理
	char* infilename = infile;      // 标准
	char* outAacname = outfile;
	/* 获取文件中的数据 */
	FILE* fpIn = fopen(infilename, "rb");
	if(NULL == fpIn)
	{
		printf("%s:[%d] open %s file failed\n",__FILE__,__LINE__,infilename);
		return -1;
	}

	FILE* fpOut = fopen(outAacname, "wb");
	if(NULL == fpOut)
	{
		printf("%s:[%d] open %s file failed\n",__FILE__,__LINE__,outAacname);
		return -1;
	}
	/* 创建缓存区 */
	int a_g711_buffer_size = 512;
	int a_aac_buffer_size = 1024;
	int gBytesRead = 0;
	unsigned int out_len = 0;
	int count = 0;
	while((gBytesRead = fread(pbG711ABuffer, 1, a_g711_buffer_size, fpIn)) >0)
	{
		if(Easy_AACEncoder_Encode(&encodectx, pbG711ABuffer, gBytesRead, pbAACBuffer, &out_len) > 0)
		{
			fwrite(pbAACBuffer, 1, out_len, fpOut);
		}
	}
	/* 释放资源 */   
	Easy_AACEncoder_Release(&encodectx);
	fclose(fpIn);
	fclose(fpOut);
    return 0;
}


/* 单独的一帧测试代码 U 的话，转成 A 的再进行转码*/
/* 一帧的 G711 进去，一帧的 AAC 出来 */
int g711_2_aac_frame_init(void *para,int infream_len)
{
	G7ToAac *encodectx = (G7ToAac *)para;
	/* AAC 编码初始化；返回G711转换成AAC的编码器 */
	Easy_AACEncoder_Init(encodectx,FRAMETYPE,infream_len);   // 按帧来处理，按帧转码的话，像 PCM缓存区 AAC缓存区,G711缓存区都不需要了，
	return 0;
}
int g711_2_aac_frame(void *para,const unsigned char * inframe_data,const int infream_len,int inframe_type,unsigned char *AacFrame_data,int *aac_len)
{
	unsigned char *g711AData;
    G7ToAac *encodectx = (G7ToAac *)para;
	int aacLen = 0;
	// /* AAC 编码初始化；返回G711转换成AAC的编码器 */
	// Easy_AACEncoder_Init(encodectx,FRAMETYPE,infream_len);   // 按帧来处理，按帧转码的话，像 PCM缓存区 AAC缓存区,G711缓存区都不需要了，
	/*检查数据是不是 G711a 的数据，不是的话转一下 */
	g711AData = (unsigned char *)malloc(sizeof(unsigned char) * infream_len);
	if(inframe_type == G711u)
	{
		for(int i = 0; i < infream_len;i++)
		{
			g711AData[i] = charulaw2alaw(inframe_data[i]);
		}
	}
	else {
		for(int i = 0; i < infream_len;i++)
		{
			g711AData[i] = inframe_data[i];
		}
	}
	/* 按帧解码之后编码 */
	if((aacLen = g711_t_aac_frame_decode(encodectx,g711AData,infream_len,AacFrame_data,aac_len)) < 0)
	{
		printf("[  %s | %d  ] : decode and encode error !!!\n");
		free(g711AData);
		return 0;
	}
	else if(aacLen == 0){
		free(g711AData);
		return 0;
	}
	free(g711AData);
	return 1;
}
int g711_2_aac_frame_uninit(void *para)
{
	G7ToAac *encodectx = (G7ToAac *)para;
	/* 释放内存 */
	Easy_AACEncoder_Release(encodectx);
}
/* 按帧转码测试代码 */
int g711_2_aac_frame_test(const char *infile, const char * outfile)
{
	char* infilename = infile;      // 标准
	char* outAacname = outfile;
	unsigned char pbG711ABuffer[512] = { 0 };
	unsigned char pbAACABuffer[1024] = { 0 };
	int aac_len = 0;
	int g711_len = 0;
	int count = 0;
	G7ToAac encodectx;
	g711_2_aac_frame_init(&encodectx,512); 
	FILE* fpIn = fopen(infilename, "rb");
	if(NULL == fpIn)
	{
		printf("%s:[%d] open %s file failed\n",__FILE__,__LINE__,infilename);
		return -1;
	}

	FILE* fpOut = fopen(outAacname, "wb");
	if(NULL == fpOut)
	{
		printf("%s:[%d] open %s file failed\n",__FILE__,__LINE__,outAacname);
		return -1;
	}
	printf("%s:[%d] start read g711 to file\n",__FILE__,__LINE__);
	while ((g711_len = fread(pbG711ABuffer, 1, 512, fpIn)) > 0)
	{
		if(g711_2_aac_frame(&encodectx,pbG711ABuffer,g711_len,G711a,pbAACABuffer,&aac_len))   /* 转码一帧 AAC 数据*/
		{
			fwrite(pbAACABuffer, 1, aac_len, fpOut);
		}
	}
	printf("%s:[%d] start free \n",__FILE__,__LINE__);
	g711_2_aac_frame_uninit(&encodectx);
	fclose(fpIn);
	fclose(fpOut);
    return 0;
}

